package com.bw.wxy.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.wxy.R;

public class TimeLineView extends View {
    private Bitmap selBitmap;//已完成签到图片
    private Bitmap popBitmap;//翻倍气泡图片
    private int selColor;
    private int normalColor;
    private String[] names={"1连签","2连签","3连签","4连签","5连签","6连签","7连签"};
    private String[] nums={"5","10","10","20","25","25","50"};
    private int width=0;//控件宽
    private int height=0;//控件高
    private int popheight=0;
    private int tvheight=ConvertUtils.dp2px(20);
    private int itemwidth=0;
    private int selindex=0;
    private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);

    public TimeLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        selBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.res_7icon_362);
        popBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.module_integral_double_bg);
        selColor= Color.RED;
        normalColor=Color.GRAY;
        popheight=popBitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getMeasuredWidth();
        height=getMeasuredHeight();
        itemwidth=width/13;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < 13; i++) {
            if (i%2==0){
                paint.setColor(normalColor);
                if (i<selindex*2){
                    paint.setColor(selColor);
                }
                if (i==6||i==12){
                    paint.setColor(selColor);
                }
                canvas.drawCircle(itemwidth*i+itemwidth/2,popheight+(height-popheight-tvheight)-tvheight/2,
                        itemwidth/2,paint);
                paint.setColor(Color.WHITE);
                canvas.drawCircle(itemwidth*i+itemwidth/2,popheight+(height-popheight-tvheight)-tvheight/2,
                        itemwidth/2- ConvertUtils.dp2px(1),paint);
                //绘制圆圈中间的签到积分
                paint.setColor(normalColor);
                if (i == 6 || i == 12) {
                    paint.setColor(selColor);
                }
                paint.setTextSize(itemwidth/2);
                paint.setFakeBoldText(true);
                paint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(nums[i/2],itemwidth*i+itemwidth/2,
                        popheight+(height-popheight-tvheight)-tvheight/2+(itemwidth/4),paint);
                paint.setColor(normalColor);
                //绘制气泡弹窗
                if (i == 6 || i == 12){
                    Rect dct = new Rect();
                    dct.left = itemwidth*i;
                    dct.right = itemwidth*i+100;
                    dct.top = 0;
                    dct.bottom = popheight/2;
                    LogUtils.json(dct.toString());
                    canvas.drawBitmap(popBitmap,null,dct,paint);
                    paint.setTextSize(itemwidth/4);
                    paint.setColor(Color.WHITE);
                    canvas.drawText("翻倍",itemwidth*i+itemwidth/2,popheight-itemwidth/2-tvheight,paint);
                    //设置连签文字颜色
                    paint.setColor(selColor);
                }
                paint.setTextSize(itemwidth/3);
                canvas.drawText(names[i/2],itemwidth*i+itemwidth/2,height-itemwidth/4,paint);
                //绘制用户已签到图片
                if (i<=selindex*2){
                    Rect rect = new Rect();
                    rect.left=itemwidth*i+ConvertUtils.dp2px(1);
                    rect.right=itemwidth*(i+1)-ConvertUtils.dp2px(1);
                    rect.top=popheight+(height-popheight-tvheight)-tvheight/2-itemwidth/2
                            +ConvertUtils.dp2px(1);
                    rect.bottom=popheight+(height-popheight-tvheight)-tvheight/2+itemwidth/2
                            -ConvertUtils.dp2px(1);
//                    LogUtils.json(rect.toString());
                    canvas.drawBitmap(selBitmap,null,rect,null);
                }
            }else {
                paint.setColor(normalColor);
                canvas.drawLine(itemwidth*i,popheight+(height-popheight-tvheight-tvheight/2)
                        -ConvertUtils.dp2px(1)/2,itemwidth*(i+1),popheight+(height-popheight-tvheight-tvheight/2)
                        +ConvertUtils.dp2px(1)/2,paint);
            }

        }
    }


//    @Override
//    protected void onDetachedFromWindow() {
//        super.onDetachedFromWindow();
//        if (selBitmap!=null){
//            selBitmap.recycle();
//        }
//        if (popBitmap!=null){
//            popBitmap.recycle();
//        }
//    }
}

